package com.example.hospital_208.controller;

import com.example.hospital_208.po.PatientDoctor;
import com.example.hospital_208.po.PatientInfo;
import com.example.hospital_208.service.PatientDoctorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/patient-doctors")
@Tag(name = "患者医生关系管理接口", description = "医院患者与医生关联信息的增删改查操作")
public class PatientDoctorController {

    private final PatientDoctorService patientDoctorService;

    public PatientDoctorController(PatientDoctorService patientDoctorService) {
        this.patientDoctorService = patientDoctorService;
    }

    // 获取所有患者医生关联记录
    @GetMapping
    @Operation(summary = "查询所有患者医生关联记录", description = "获取医院所有患者与医生的关联信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientDoctor.class)))
    })
    public List<PatientDoctor> getAllPatientDoctors() {
        return patientDoctorService.listPatientDoctors();
    }

    // 根据医生ID获取医生姓名
    @GetMapping("/doc-name/{doc_id}")
    @Operation(summary = "根据医生ID获取医生姓名", description = "通过doc_id查询对应的医生姓名")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "404", description = "未找到对应的医生")
    })
    public String getDocNameByDocId(@PathVariable("doc_id")
                                    @Parameter(description = "医生ID", example = "DOC001", required = true)
                                    String doc_id) {
        return patientDoctorService.getDocNameByDocId(doc_id);
    }

    // 根据ID获取患者医生关联记录详情
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询患者医生关联记录", description = "通过记录ID获取详细关联信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientDoctor.class))),
            @ApiResponse(responseCode = "404", description = "记录不存在")
    })
    public PatientDoctor getPatientDoctor(@PathVariable("id")
                                          @Parameter(description = "记录ID", example = "1", required = true)
                                          Integer id) {
        return patientDoctorService.getPatientDoctorById(id);
    }

    // 新增患者医生关联记录
    @PostMapping
    @Operation(summary = "新增患者医生关联记录", description = "向系统中添加患者与医生的关联信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误")
    })
    public String addPatientDoctor(@RequestBody
                                   @Parameter(description = "患者医生关联实体对象",
                                           content = @Content(schema = @Schema(implementation = PatientDoctor.class)))
                                   PatientDoctor patientDoctor) {
        int result = patientDoctorService.addPatientDoctor(patientDoctor);
        return result > 0 ? "患者医生关联记录添加成功" : "患者医生关联记录添加失败";
    }

    // 修改患者医生关联记录
    @PutMapping
    @Operation(summary = "修改患者医生关联记录", description = "更新已有患者与医生的关联信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或记录不存在")
    })
    public String updatePatientDoctor(@RequestBody
                                      @Parameter(description = "包含更新信息的患者医生关联实体",
                                              content = @Content(schema = @Schema(implementation = PatientDoctor.class)))
                                      PatientDoctor patientDoctor) {
        int result = patientDoctorService.editPatientDoctor(patientDoctor);
        return result > 0 ? "患者医生关联记录修改成功" : "患者医生关联记录修改失败";
    }

    // 删除患者医生关联记录
    @DeleteMapping("/{id}")
    @Operation(summary = "删除患者医生关联记录", description = "根据ID删除患者与医生的关联信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或记录存在关联数据")
    })
    public String deletePatientDoctor(@PathVariable("id")
                                      @Parameter(description = "待删除的记录ID", example = "1", required = true)
                                      Integer id) {
        int result = patientDoctorService.removePatientDoctor(id);
        return result > 0 ? "患者医生关联记录删除成功" : "患者医生关联记录删除失败";
    }

    // 根据名字查询患者医生关联记录
    @GetMapping("/name/{name}")
    @Operation(summary = "根据名字查询患者医生关联记录", description = "通过名字查询对应的患者医生关联记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientDoctor.class))),
            @ApiResponse(responseCode = "404", description = "未找到对应的记录")
    })
    public List<PatientDoctor> getPatientDoctorsByName(@PathVariable("name")
                                                       @Parameter(description = "患者姓名", example = "李明华", required = true)
                                                       String name) {
        return patientDoctorService.getPatientDoctorsByName(name);
    }

    // 根据医生ID获取患者信息
    @GetMapping("/patients/{doc_id}")
    @Operation(summary = "根据医生ID获取患者信息", description = "通过医生ID查询对应的患者信息列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientDoctor.class))),
            @ApiResponse(responseCode = "404", description = "未找到对应的患者信息")
    })
    public List<PatientDoctor> getPatientsByDocId(@PathVariable("doc_id")
                                                  @Parameter(description = "医生ID", example = "20200201D003", required = true)
                                                  String doc_id) {
        return patientDoctorService.getPatientsByDocId(doc_id);
    }
    // 新增：修改就诊状态接口
    @PutMapping("/status/{id}")
    @Operation(summary = "修改就诊状态", description = "根据记录ID修改患者的就诊状态（已就诊/未就诊）")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功", content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或记录不存在")
    })
    public String updateStatus(
            @PathVariable("id") @Parameter(description = "记录ID", example = "1") Integer id,
            @RequestParam("status") @Parameter(description = "就诊状态（已就诊/未就诊）", example = "已就诊") String status) {
        int result = patientDoctorService.updateStatusById(id, status);
        return result > 0 ? "就诊状态修改成功" : "就诊状态修改失败";
    }
    // 通过patient_id关联patient_info表查询信息
    @GetMapping("/patient-info/{patient_id}")
    @Operation(summary = "通过patient_id关联patient_info表查询信息", description = "通过patient_id查询对应的患者信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = PatientInfo.class))),
            @ApiResponse(responseCode = "404", description = "未找到对应的患者信息")
    })
    public PatientInfo getPatientInfoByPatientId(@PathVariable("patient_id")
                                                 @Parameter(description = "患者ID", example = "P001", required = true)
                                                 String patient_id) {
        return patientDoctorService.getPatientInfoByPatientId(patient_id);
    }
}